package com.yoomiito.app.address

import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.base.AppConfig
import com.yoomiito.app.base.BaseActPresenter
import com.yoomiito.app.model.bean.AddressBean
import com.yoomiito.app.model.bean.AddressInfo
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.showToast

class EditAddressPresenter(appConfig: AppConfig) : BaseActPresenter<EditAddressActivity>(appConfig){
    fun addAddress(addressInfo: AddressInfo){
        YmApi.getYmService()
                .addAddress(addressInfo.userName,
                        addressInfo.mobile,
                        addressInfo.isDefault,
                        addressInfo.provinceCode,
                        addressInfo.cityCode,
                        addressInfo.countyCode,
                        addressInfo.provinceName,
                        addressInfo.cityName,
                        addressInfo.countyName,
                        addressInfo.address)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<AddressBean>(){
                    override fun onSuccess(t: AddressBean?) {
                        "保存成功".showToast()
                        val addressInfo1 = AddressInfo()
                        addressInfo1.id = t?.id
                        addressInfo1.userName = addressInfo?.userName
                        addressInfo1.mobile = addressInfo?.mobile
                        addressInfo1.provinceName = addressInfo?.provinceName
                        addressInfo1.cityName = addressInfo?.cityName
                        addressInfo1.countyName = addressInfo?.countyName
                        addressInfo1.address = addressInfo?.address
                        addressInfo1.isDefault = addressInfo?.isDefault
                        addressInfo1.provinceCode = addressInfo?.provinceCode
                        addressInfo1.cityCode = addressInfo?.cityCode
                        addressInfo1.countyCode = addressInfo?.countyCode
                        v.success(addressInfo1)
                    }

                    override fun onFail(error: ApiException?) {
                        v.dismissLoading()
                        error?.msg?.showToast()
                    }
                })
    }

    fun updateAddress(addressInfo: AddressInfo){
        YmApi.getYmService()
                .updateAddress(addressInfo.id,
                        addressInfo.userName,
                        addressInfo.mobile,
                        addressInfo.isDefault,
                        addressInfo.provinceCode,
                        addressInfo.cityCode,
                        addressInfo.countyCode,
                        addressInfo.provinceName,
                        addressInfo.cityName,
                        addressInfo.countyName,
                        addressInfo.address)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerNoData<BaseModel<*>>())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<BaseModel<*>>(){
                    override fun onSuccess(t: BaseModel<*>?) {
                        "保存成功".showToast()
                        v.success(null)
                    }

                    override fun onFail(error: ApiException?) {
                        v.dismissLoading()
                        error?.msg?.showToast()
                    }
                })
    }

//    fun getAddress() {
//        YmApi.getYmService()
//                .defaultAddress
//                .compose(v.bindToLifecycle())
//                .compose(XApi.getApiTransformerData())
//                .compose(XApi.getScheduler())
//                .subscribe(object : ApiSubscriber<AddressInfo>() {
//                    override fun onSuccess(addressInfo: AddressInfo) {
//                        "保存成功".showToast()
//                        v.success(addressInfo)
//                        //                        if (addressInfos.size()==0){
//                        //                        }else {
//                        //                            for (AddressInfo addressInfo:addressInfos) {
//                        //                                if ("1".equals(addressInfo.getIsDefault())){
//                        //                                    getV().showAddressView(addressInfo);
//                        //                                }
//                        //                            }
//                        //                        }
//                    }
//
//                    override fun onFail(error: ApiException) {
//                        v.dismissLoading()
//                        if (!TextUtils.isEmpty(error.msg)) {
//                            YMToast.showText(error.msg)
//                        }
//                    }
//                })
//    }

    fun delAddress(id:String){
        YmApi.getYmService()
                .delAddress(id)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerNoData<BaseModel<*>>())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<BaseModel<*>>(){
                    override fun onSuccess(t: BaseModel<*>?) {
                        v.success(null)
                    }

                    override fun onFail(error: ApiException?) {
                        error?.msg?.showToast()
                    }

                })
    }
}
